const { Controller } = require("egg");


class DirectoryController extends Controller {
  async list() {
    const { ctx, service } = this;
    let { directoryId, search = "", pageNum = 1, pageSize = 10 } = ctx.query;
    if (!directoryId) {
      return this.ctx.helper.error(`参数异常，directoryId不能为空`);
    }
    const isExistsDirectory = await service.directory.findOne({
      id: directoryId,
    });
    if (!isExistsDirectory) {
      return this.ctx.helper.error("当前栏目不存在, 查询失败");
    }

    pageNum = parseInt(pageNum);
    pageSize = parseInt(pageSize);

    try {
      const res = await service.directoryArticle.listQuery({
        directoryId,
        pageNum,
        pageSize,
        search,
      });
      this.ctx.helper.success(res);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  
  async getDataByIds() {
    const { ctx, service } = this;
    const { ids } = ctx.request.body;
    const res = await service.directoryArticle.getDataByIds(ids);
    this.ctx.helper.success(res);
  }

  async bindOrUpdate() {
    const { ctx, service } = this;
    let { articleId, directoryId, type, ...others } = ctx.request.body;
    if (!directoryId) {
      return this.ctx.helper.error(`参数异常，directoryId不能为空`);
    }
    let res;
    if (type === 'edit' && articleId) {
      const data = {
        title: others.title,
        detail: others.detail,
        classify: others.classify,
        publishTime: others.publishTime,
        poster: others.poster,
      }
      res = await service.article.update({ id: articleId }, data);
    } else {
      res = await service.article.create({ ...others });
      articleId = res.id
    }

    const isExists = await service.directoryArticle.findOne({
      articleId,
      directoryId,
    });

    if (isExists) {
      return this.ctx.helper.success(isExists);
    } else {
      try {
        const result = await service.directoryArticle.bind({
          articleId,
          directoryId,
        });
        return this.ctx.helper.success(result);
      } catch (error) {
        return this.ctx.helper.error(error);
      }
    }

  }

  async delete() {
    const { ctx, service } = this;
    const { articleId, directoryId } = ctx.request.body;
    if (!directoryId || !articleId) {
      return this.ctx.helper.error(`参数异常，directoryId 和articleId不能为空`);
    }

    try {
      const result = await service.directoryArticle.delete(
        articleId,
        directoryId
      );
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async setTop() {
    const { ctx, service } = this;
    const { articleId, directoryId } = ctx.request.body;
    if (!directoryId || !articleId) {
      return this.ctx.helper.error(`参数异常，directoryId 和articleId不能为空`);
    }
    try {
      const result = await service.directoryArticle.setTop({
        articleId,
        directoryId,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }

  async cancelTop() {
    const { ctx, service } = this;
    const { articleId, directoryId } = ctx.request.body;
    if (!directoryId || !articleId) {
      return this.ctx.helper.error(`参数异常，directoryId 和articleId不能为空`);
    }
    try {
      const result = await service.directoryArticle.cancelTop({
        articleId,
        directoryId,
      });
      this.ctx.helper.success(result);
    } catch (error) {
      this.ctx.helper.error(error);
    }
  }
}

module.exports = DirectoryController;
